package com.project.bookstore.stock.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.bookstore.book.mapper.BookMapper;
import com.project.bookstore.book.pojo.BookDetailVO;
import com.project.bookstore.common.State;
import com.project.bookstore.exception.ServiceException;
import com.project.bookstore.stock.domain.Stock;
import com.project.bookstore.stock.pojo.StockAddNewVO;
import com.project.bookstore.stock.service.StockService;
import com.project.bookstore.stock.mapper.StockMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

import static com.project.bookstore.common.State.INTERNAL_EXCEPTION;

/**
 * @author 12603
 * @description 针对表【stock】的数据库操作Service实现
 * @createDate 2022-04-17 16:49:27
 */
@Service
@Slf4j
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock>
        implements StockService {

    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private BookMapper bookMapper;

    @Override
    public Long addNew(StockAddNewVO stockAddNewVO) {
        BookDetailVO bookDetailVO = bookMapper.selectByBookName(stockAddNewVO.getBookName());
        log.debug("根据书名找书:{}",bookDetailVO);
        BookDetailVO newBookDetailVO = new BookDetailVO();
        BeanUtils.copyProperties(stockAddNewVO, newBookDetailVO);
        Long bookId;
        Integer newPCS = stockAddNewVO.getCount()+stockAddNewVO.getPCS();
        //如果 没有查到数据 或者 书名、作者、出版社 有任意一个不相同就是插入新的书
        if (bookDetailVO == null ||//没有查到数据
                !bookDetailVO.getBookName().equals(stockAddNewVO.getBookName()) ||
                !bookDetailVO.getAuthor().equals(stockAddNewVO.getAuthor()) ||
                !bookDetailVO.getPress().equals(stockAddNewVO.getPress())) {
            newBookDetailVO.setCreated(LocalDateTime.now())
                    .setModified(LocalDateTime.now())
                    .setPCS(newPCS);
            log.debug("插入新的书：{}", newBookDetailVO);
            int i2 = bookMapper.insertBook(newBookDetailVO);
            bookId = newBookDetailVO.getId();
            log.debug("插入的书的ID：{}",bookId);
        } else {
            //直接复制一个 调用更新方法
            bookId = bookDetailVO.getId();
            BeanUtils.copyProperties(stockAddNewVO, bookDetailVO);
            bookDetailVO.setPCS(newPCS).
                    setModified(LocalDateTime.now())
                    .setId(bookId);
            log.debug("更新书:{}", bookDetailVO);
            int i = bookMapper.updateBookDetailById(bookDetailVO);
            if (i != 1) {
                log.warn("入库的时候更新书籍,{}发生未知错误", bookDetailVO);
                throw new ServiceException(INTERNAL_EXCEPTION,"服务器忙请重试");
            }
        }
        //复制属性到stock上设置好bookId插入到库存表
        Stock stock = new Stock();
        BeanUtils.copyProperties(stockAddNewVO, stock);
        stock.setBookId(bookId)
                .setId(null)
                .setCreated(LocalDateTime.now())
                .setModified(LocalDateTime.now())
                .setPCS(newPCS);
        log.debug("插入库存：,{}", stock);
        int insert = stockMapper.insertStock(stock);
        if (insert != 1) {
            log.warn("插入库存失败出现未知错误");
            throw new ServiceException(INTERNAL_EXCEPTION,"服务器忙请重试");
        }
        return null;
    }

}




